package com.vancouver.knows.ui.fragment.homePage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.pangu.util.StringUtil;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.ui.activity.WebViewActivity;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.ui.fragment.homePage.carousel.CycleViewPager;
import com.vancouver.knows.ui.fragment.homePage.carousel.ViewFactory;
import com.vancouver.knows.ui.fragment.homePage.carousel.CycleViewPager.ImageCycleViewListener;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.HomePageCarouseInfo;
import com.vancouver.knows.business.service.impl.HomePageHandle;

public class HomepageCarouselView {
	private Activity mContext;
	@Bind(R.id.indicator_desc)
	TextView imageText;
	@Bind(R.id.layout_indicator)
	LinearLayout indicatorlayout;
	@Bind(R.id.fragment_cycle_viewpager_content)
	CycleViewPager cycleViewPager;

	private LayoutInflater mInflater;
	private View mCarouselView;
	private BusinessModule bModule;

	private List<ImageView> views = new ArrayList<ImageView>();
	private static List<HomePageCarouseInfo> homapageCarouseLst;
	private static HashMap<String, Object> homePageMap;

	public HomepageCarouselView(Activity context, LayoutInflater inflater) {
		mContext = context;
		mInflater = inflater;
		this.bModule = ((MainApplication) context.getApplication()).getBusinessModule();
		setViews();
	}

	private void setViews() {
		if (mCarouselView == null) {
			mCarouselView = mInflater.inflate(R.layout.carousel_view, null);
		}
		ButterKnife.bind(this, mCarouselView);

	}

	@SuppressWarnings("unchecked")
	private void getHomePageData() {
		homePageMap = bModule.getCacheManager().getHomePageMap();
		if (homePageMap == null) {
			return;
		}
		homapageCarouseLst = (List<HomePageCarouseInfo>) homePageMap.get(HomePageHandle.HOMEPAGE_CAROUS);
	}

	@SuppressLint("NewApi")
	public void initialize() {
		getHomePageData();
		if (homapageCarouseLst == null || homapageCarouseLst.size() == 0) {
			return;
		}
		if (views != null) {
			views.clear();
		}

		// 将最后一个ImageView添加进来
		views.add(ViewFactory.getImageView(mContext, homapageCarouseLst.get(homapageCarouseLst.size() - 1)
				.getBannerImg()));
		for (int i = 0; i < homapageCarouseLst.size(); i++) {
			views.add(ViewFactory.getImageView(mContext, homapageCarouseLst.get(i).getBannerImg()));
		}
		// 将第一个ImageView添加进来
		views.add(ViewFactory.getImageView(mContext, homapageCarouseLst.get(0).getBannerImg()));
		setCycleViewPager();
	}

	private void setCycleViewPager() {
		// 设置循环，在调用setData方法前调用
		cycleViewPager.setCycle(true);
		// 在加载数据前设置是否循环
		cycleViewPager.setData(views, homapageCarouseLst, mAdCycleViewListener, indicatorlayout, imageText);
		// 设置轮播
		cycleViewPager.setWheel(true);
		// 设置轮播时间，默认5000ms
		cycleViewPager.setTime(2000);
	}

	private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

		@Override
		public void onImageClick(HomePageCarouseInfo info, int position, View imageView) {
			if (cycleViewPager.isCycle()) {
				position = position - 1;
				if (HomePageCarouseInfo.TYPE_WEB.equals(info.getBannerType())
						&& !StringUtil.isEmptyString(info.getBannerUrl())) {
					Intent webIntent =  new Intent(Intent.ACTION_VIEW);
					webIntent.setData(Uri.parse(info.getBannerUrl()));
					mContext.startActivity(webIntent);
				} else if (HomePageCarouseInfo.TYPE_APPLICATION.equals(info.getBannerType())
						&& !StringUtil.isEmptyString(info.getPostId())) {
					inAppJumpToDetail(info);
				}

				Tracker t = ((MainApplication) mContext.getApplication()).getTracker(TrackerName.APP_TRACKER);
				t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_HOMEPAGE);
				t.send(new HitBuilders.EventBuilder().setCategory(AnalyticsConstants.CATEGORY_HOMEPAGE_BANNER)
						.setAction(AnalyticsConstants.ACTION_ONCLICK)
						.setLabel(AnalyticsConstants.LABEL_HOMEPAGE_BANNER + info.getBannerTitle()).build());
			}
		}
	};

	/**
	 * 应用内跳转
	 * @param info
     */
	private void inAppJumpToDetail(HomePageCarouseInfo info){
		if (info != null) {
			Class cla = null;
			String postType = info.getPostType();
			if ("post".equals(postType)) {
				cla = CityNewsDetailActivity.class;
			} else if ("event".equals(postType)) {
				cla = EventDetailActivity.class;
			} else if ("business".equals(postType)) {
				cla = CityDiscountDetailActivity.class;
			}
			if (cla != null) {
				Intent intent = new Intent(mContext, cla);
				intent.putExtra("postId", info.getPostId());
				mContext.startActivity(intent);
			}
		}
	}

	public View getCarouselView() {
		return mCarouselView;
	}
}
